package com.hrbxlh.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hrbxlh.actionImpl.CourseActionImpl;
import com.hrbxlh.actionImpl.DepartMentActionImpl;
import com.hrbxlh.actionImpl.SiteActionImpl;


/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：SiteAction.java
* Description：上课地点action类
* History：
* Date           Author               Desc
* 2017.04.11	  xzx                  建立	
* -------------------------------------------------------
*/
@Controller
public class SiteAction {
	
	@Autowired
	@Qualifier("siteActionImpl")
	private SiteActionImpl siteActionImpl;
	
	@Autowired
	@Qualifier("courseActionImpl")
	private CourseActionImpl courseActionImpl;
	
	@Autowired
	@Qualifier("departMentActionImpl")
	private DepartMentActionImpl departMentActionImpl;
	
	/**xzx
	 * @description 地点管理列表
	 * @param request
	 * @param response
	 * //Map<String, Object> mapUse=new HashMap<String, Object>();
		//mapUse.put("pageNow", 1);
		//mapUse.put("pageSize", 10);
		//jsonStr = mapper.writeValueAsString(mapUse);
	 * @return  跳转到地点管理列表页面
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/skipSiteMana", method = RequestMethod.GET)
	public ModelAndView skipSiteMana(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(SiteAction.class.getName());
		try {
			Map<String,Object> preParamMap = new HashMap<String, Object>();
			//获取参数值
			if(jsonStr != null){
				preParamMap = mapper.readValue(jsonStr, resultMap.getClass());
			}
			//获取地点列表
			Map<String, Object> siteMap = siteActionImpl.getSiteList(preParamMap);
			if(siteMap != null){
				resultMap.put("siteList", siteMap.get("siteList"));
				resultMap.put("totalPage", siteMap.get("totalPage"));
				resultMap.put("pageNow", siteMap.get("pageNow"));
			}
			//返回条件
			resultMap.put("pageSize", preParamMap.get("pageSize"));
			//获取我的消息
			List<Map<String, Object>> noticeList  = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
			//全部院系
			List<Map<String, Object>> departmentList = departMentActionImpl.getDepartmentAll(new HashMap<String, Object>());
			resultMap.put("departmentList", departmentList);
			resultMap.put("departmentId", preParamMap.get("departmentId"));
			//地点管理页面名称
			pageName = "pc/siteMana";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳地点管理页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**xzx
	 * @description 进入添加地点页面
	 * @param request
	 * @param response
	 * @return  跳转到地点管理列表页面
	 * @throws IOException
	 */
	@RequestMapping(value = "/skipAddSite", method = RequestMethod.GET)
	public ModelAndView skipAddSite(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(SiteAction.class.getName());
		try {
			//获取我的消息
			List<Map<String, Object>> noticeList  = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
			//添加地点
			pageName = "pc/addSite";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳添加地点页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**xzx
	 * @dscription 添加地点
	 * @param request
	 * @param response
	//Map<String, Object> mapUse=new HashMap<String, Object>();
	//mapUse.put("siteName", "哼哼哼");
	//jsonStr = mapper.writeValueAsString(mapUse);
	 * @return  地点名称
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addSite", method = RequestMethod.POST)
	public void  addSite (HttpServletResponse response, HttpServletRequest request, String jsonStr) throws IOException {

		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(SiteAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try { 
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//添加地点信息
			Map<String, Object> addResultMap = siteActionImpl.addSite(paramMap);
			Boolean flag = (Boolean)addResultMap.get("result");
			//返回参数
			resultMark = (flag) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(resultMark.equals("fail")){
				resultMap.put("failMsg", addResultMap.get("failMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("添加地点出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**xzx
	 * @description 进入修改地点页面
	 * @param request
	 * @param response
	 //Map<String, Object> mapUse=new HashMap<String, Object>();
	//mapUse.put("siteId", 1);
	//jsonStr = mapper.writeValueAsString(mapUse);
	 * @return  跳转到修改地点页面
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/skipSaveSite", method = RequestMethod.GET)
	public ModelAndView skipSaveSite(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(SiteAction.class.getName());
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			Map<String,Object> siteDetail = siteActionImpl.getSiteDetail(paramMap);
			resultMap.put("siteDetail", siteDetail);
			//获取我的消息
			List<Map<String, Object>> noticeList  = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
			//修改地点页面
			pageName = "pc/saveSite";
		
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳修改地点页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**xzx
	 * @description 修改地点
	 * @param request
	 * @param response
	 //Map<String, Object> mapUse=new HashMap<String, Object>();
	//mapUse.put("siteName", "wawawaawaw");
	//mapUse.put("siteId", 1);
	//jsonStr = mapper.writeValueAsString(mapUse);
	 * @return  地点名称
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveSite", method = RequestMethod.POST)
	public void  saveSite (HttpServletResponse response, HttpServletRequest request, String jsonStr) throws IOException {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(SiteAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try { 
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//修改地点信息
			Map<String, Object> saveResultMap = siteActionImpl.saveSite(paramMap);
			Boolean flag = (Boolean)saveResultMap.get("result");
			//返回参数
			resultMark = (flag) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(resultMark.equals("fail")){
				resultMap.put("failMsg", saveResultMap.get("failMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("修改地点出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**xzx
	 * @description 删除地点
	 * @param request
	 * @param response
	 //Map<String, Object> mapUse=new HashMap<String, Object>();
	//mapUse.put("siteId", 1);
	//jsonStr = mapper.writeValueAsString(mapUse);
	 * @return  地点id
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delSite", method = RequestMethod.POST)
	public void  delSite (HttpServletResponse response, HttpServletRequest request, String jsonStr) throws IOException {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(SiteAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try { 
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//删除院系信息
			Map<String, Object> result = siteActionImpl.delSite(paramMap);
			boolean resultMarkUse = (Boolean)result.get("result");
			//返回参数
			resultMark = (resultMarkUse) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(!resultMarkUse){
				resultMap.put("failMsg",result.get("failMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除地点出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
}